package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.AlbumMqService;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * ClassName: AlbumMqServiceImpl
 * Package: com.atguigu.tingshu.album.service.impl
 * Description:
 *
 * @Author 梅超凡
 * @Create 2025/2/5 20:40
 * @Version 1.0
 */
@Service
@Slf4j
public class AlbumMqServiceImpl implements AlbumMqService {
    @Autowired
    private AlbumStatMapper albumStatMapper;

    @Autowired
    private TrackStatMapper trackStatMapper;

    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    /**
     * 监听专辑播放数和声音播放数更新
     * @param trackStatMqVo
     */
    @Override
    public void listenAlbumAndTrackStatUpdate(TrackStatMqVo trackStatMqVo) {
        Long albumId = trackStatMqVo.getAlbumId();
        Long trackId = trackStatMqVo.getTrackId();
        String statType = trackStatMqVo.getStatType();
        Integer count = trackStatMqVo.getCount();
        try{
            //1.更新专辑播放数
            int i = albumStatMapper.updateAlbumStatNum(albumId,statType,count);
            if (i > 0){
                log.info("更新专辑播放数成功");
            } else {
                log.warn("更新专辑播放数失败");
            }

            //2.更新声音播放数
            int j = trackStatMapper.updateTrackPlayStatNum(trackId, SystemConstant.TRACK_STAT_PLAY,count);
            if (j > 0){
                log.info("更新声音播放数成功");
            } else {
                log.error("更新声音播放数失败");
            }
        }catch (Exception e){
            throw new GuiguException(20001,"数据表更新失败");
        }



    }

    /**
     * 修改专辑购买量
     * @param orderNo
     */
    @Override
    public void updateAlbumAndBuyNum(String orderNo) {

        // 1.根据订单编号查询订单信息获取订单类型
        OrderInfo orderInfoData = orderInfoFeignClient.getOrderInfoByOrderNo(orderNo).getData();
        Assert.notNull(orderInfoData, "远程查询订单信息失败");
        //2.获取订单的类型
        String itemType = orderInfoData.getItemType();
        //3.修改播放量
        //初始化专辑id
        Long albumId = null;
        //4.判断订单类型
        if(SystemConstant.ORDER_ITEM_TYPE_ALBUM.equals(itemType)){
            //如果是专辑订单，获取专辑id
            albumId = orderInfoData.getOrderDetailList().get(0).getItemId();
        }
        if(SystemConstant.ORDER_ITEM_TYPE_TRACK.equals(itemType)){
            //如果是声音订单
            //
            Long trackId = orderInfoData.getOrderDetailList().get(0).getItemId();
            TrackInfo trackInfoData = albumInfoFeignClient.getTrackInfoById(trackId).getData();
            Assert.notNull(trackInfoData, "远程查询声音信息失败");
            albumId = trackInfoData.getAlbumId();
        }
        if(SystemConstant.ORDER_ITEM_TYPE_VIP.equals(itemType)){
            //如果是vip订单，es不用处理
        }
        if(albumId != null){
            //修改购买量
            albumStatMapper.updateAlbumStatNum(albumId, SystemConstant.ALBUM_STAT_BROWSE, orderInfoData.getOrderDetailList().size());
        }
    }
}
